//
//  RHReportElectricityStatisticsCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/11/15.
//

#import "RHReportStatisticsCell.h"
@interface RHReportStatisticsCell()


@end
@implementation RHReportStatisticsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    self.contentView.backgroundColor = CFTabViewBgColor;
    //---------------------- 日电量
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(15);
    }];
    
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_report_day_electricity"]];
    [bgView addSubview:iconImg];
    
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"日电量".rh_localized font:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView addSubview:titleLab];
   
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
//    self.itemsArray = [NSMutableArray array];
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    __block UIView *lastview;
    NSArray *titles = @[@"日发电量".rh_localized,@"日放电量".rh_localized];
    __block CGFloat itemWidth = (kUI_WINDOW_W - 30)/2 - 25;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView  = [self subItimeViewWithTitle:obj index:idx];
//        itemView.backgroundColor = kRandomColor;
        [itemBgView addSubview:itemView];
//        [self.itemsArray addObject:itemView];
        if (idx == 0) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(20);
                make.top.mas_offset(0);
                make.width.offset(itemWidth);
                make.height.offset(65);
            }];
        }else{
            
            if (idx % 2) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastview);
                    make.left.equalTo(itemBgView.mas_centerX).offset(20);
                    make.top.equalTo(lastview);
                }];
            }else{
                //换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(20);
                    make.top.equalTo(lastview.mas_bottom).offset(1);
                    make.size.equalTo(lastview);
                }];
                //横线
                UIView *line = kView;
                line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
                [itemBgView addSubview:line];
                [line mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(1);
                    make.left.mas_offset(20);
                    make.right.mas_offset(-20);
                    make.top.equalTo(lastview.mas_bottom);
                }];
            }
            
        }
        //竖线
        if ( idx%2 == 0) {
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
            [itemView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(1);
                make.height.offset(35);
                make.centerY.mas_offset(0);
                make.right.mas_offset(0);
            }];
        }
        lastview = itemView;
    }];
    //封底
    [lastview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    
    //---------------------- 月电量
    UIView *bgView2 = kView;
    bgView2.backgroundColor = UIColor.whiteColor;
    bgView2.layer.cornerRadius = 8;
    bgView2.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView2];
    [bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(bgView.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
      
    }];
    
    UIImageView *iconImg2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_report_total_electricity"]];
    [bgView2 addSubview:iconImg2];
    
    [iconImg2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    
    QMUILabel *titleLab2 = [QMUILabel labelWithText:@"月电量".rh_localized font:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView2 addSubview:titleLab2];
   
    [titleLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg2.mas_right).offset(10);
        make.centerY.equalTo(iconImg2);
    }];
    
    UIView *itemBgView2 = kView;
    [bgView2 addSubview:itemBgView2];
    [itemBgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg2.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
//    self.itemsArray = [NSMutableArray array];
  
    __block UIView *lastview2;
    NSArray *titles2 = @[@"月发电量".rh_localized,@"月放电量".rh_localized];
    [titles2 enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView  = [self subItimeViewWithTitle:obj index:idx];
//        itemView.backgroundColor = kRandomColor;
        [itemBgView2 addSubview:itemView];
//        [self.itemsArray addObject:itemView];
        if (idx == 0) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(20);
                make.top.mas_offset(0);
                make.width.offset(itemWidth);
                make.height.offset(65);
            }];
        }else{
            
            if (idx % 2) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastview2);
                    make.left.equalTo(itemBgView2.mas_centerX).offset(20);
                    make.top.equalTo(lastview2);
                }];
            }else{
                //换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(20);
                    make.top.equalTo(lastview2.mas_bottom).offset(1);
                    make.size.equalTo(lastview2);
                }];
                //横线
                UIView *line = kView;
                line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
                [itemBgView2 addSubview:line];
                [line mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(1);
                    make.left.mas_offset(20);
                    make.right.mas_offset(-20);
                    make.top.equalTo(lastview2.mas_bottom);
                }];
            }
            
        }
        //竖线
        if ( idx%2 == 0) {
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
            [itemView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(1);
                make.height.offset(35);
                make.centerY.mas_offset(0);
                make.right.mas_offset(0);
            }];
        }
        lastview2 = itemView;
    }];
    //封底
    [lastview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
}






-(UIView*)subItimeViewWithTitle:(NSString *)title index:(NSInteger)index
{
    UIView *itemView = kView;
    QMUILabel *subTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#3F4443",0.6) textAligment:NSTextAlignmentLeft];
    subTitleLab.text = title.rh_localized;
    subTitleLab.tag = 100+index;
    subTitleLab.numberOfLines = 2;
//    subTitleLab.adjustsFontSizeToFitWidth = YES;
//    subTitleLab.minimumScaleFactor = 0.8;
    [itemView addSubview:subTitleLab];
    
    QMUILabel *contentLab = [QMUILabel labelWithFont:kSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentLeft];
    contentLab.text = @"--(kWh)";
    contentLab.tag = 200+index;
    [itemView addSubview:contentLab];
    
    [subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.top.equalTo(itemView.mas_centerY).offset(3);
        
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView.mas_centerY).offset(-3);
    }];
    return itemView;
}


@end
